<?php

namespace app\api\model;

use think\Db;
use think\Model;

class LearningImages extends Model
{
    public static function createImage($learning_id, $images, $type)
    {
        $data = [];
        foreach ($images as $image) {
            $data[] = [
                'learning_id' => $learning_id,
                'image' => $image,
                'type' => $type
            ];
        }
        return self::insertAll($data);
    }

    public static function updateImages($learning_id, $images, $type)
    {
        Db::transaction(function () use ($learning_id, $images, $type){
            self::where('learning_id', $learning_id)->where('type', $type)->delete();
            if ($images) {
                self::createImage($learning_id, $images, $type);
            }
        });
    }

}
